package com.jsgt.greekfire.ui.wallet.export.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseFragment
import com.fhx.core.utils.QRCodeUtils
import com.fhx.core.utils.ScreenUtil
import com.google.gson.Gson
import com.jsgt.greekfire.R
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_receive.*
import org.web3j.utils.Numeric
import java.math.BigInteger

private const val ARG_PARAM1 = "param1"

class KeyQRCodeFragment : BaseFragment() {
    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(requireActivity()).get(WalletViewModel::class.java)
    }
    private val height by lazy { ScreenUtil.dip2px(requireContext(), 200f).toInt() }
    private var param1: Int = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getInt(ARG_PARAM1)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_key_q_r_code
    }

    override fun main() {
        walletViewModel.getIdentity(param1)?.observe(this, Observer {
            val keyStr = Numeric.toHexStringNoPrefix(BigInteger(it.getPriKeyByAES()))
            val codeBitmap = QRCodeUtils.createQRCodeBitmap(keyStr, height, height)
            imvQR.setImageBitmap(codeBitmap)
        })
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: Int) =
            KeyQRCodeFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_PARAM1, param1)
                }
            }
    }
}